/**
 * Created by yanl_ on 2018/1/5.
 */
window.onload = function () {
    //easyui 默认格式化日期格式eg 2018/01/01
    $('#startTime').datebox('setValue', formatterDate(new Date()));
    $('#endTime').datebox('setValue', formatterDate(new Date()));

}


function channeSearch() {

    var startDay = $("#startTime").datebox('getValue');
    var endDay = $("#endTime").datebox('getValue');
    var channelName = $("#channelName").combobox("getText");
    var currentDay = formatterDate(new Date());
    // var channelName = "intrajasa";
    //校验时间是否有效
    if (!validatiorDay(startDay) || !validatiorDay(endDay)) {
        return false;
    }

    if (channelName == "" || channelName == null) {
        $.messager.alert('提示', '通道不能为空');
        return false;
    }

    //校验开始结束时间
    var startTime = Date.parse(startDay);
    var endTime = Date.parse(endDay);
    var currentTime = Date.parse(currentDay);
    var days = Math.abs(startTime - endTime);
    var times = parseInt(days / (1000 * 60 * 60 * 24));

    if ((startTime > endTime) || (times > 15) || ((currentTime - endTime) < 0)) {
        $.messager.alert('提示', '起始时间不能大于结束时间,时间差不能大于15天或清算结束日期不能大于当前日期');
        return false;
    }

    var jsonStr = {
        "startDay": startDay,
        "endDay": endDay,
        "channelName": channelName,
        "handlerType": 1
    };


    //调用almod对账接口
    $.ajax({
        type: "POST",
        url: "easyui",
        async: false,
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(jsonStr),
        dataType: "json",

        error: function (data) {
            $.messager.alert("对账清处理出错,请联系管理员", data);
        },
        success: function (data) {
            var json = data.rows;

            $.each(json, function (index) {
                //循环获取数据
                var time = json[index].time;
                var flag = json[index].flag;
                var channelName = json[index].channelName;

                $.messager.alert("执行成功", "对账处理中,请从历史清算结果中查看清算结果" + time + "  " + flag + "  " + channelName);
            });


        }
    });


}


function unliquidationSearch() {

    var row = $('#liquidtionDatagridFrom').datagrid('getSelected');
    if (row) {
        var rowIndex = $('#liquidtionDatagridFrom').datagrid('getRowIndex', row);

        var jsonStr = {
            "startDay": row.time,
            "channelName": row.channelName,
            "handlerType": 2
        };

        $.ajax({
            type: "POST",
            url: getRootPath() + "/accountLiquidation/query",
            async: false,
            contentType: 'application/json;charset=utf-8',
            data: JSON.stringify(jsonStr),
            dataType: "json",

            error: function (data) {
                $.messager.alert("对账清处理出错,请联系管理员", data);
            },
            success: function (data) {

                $('#liquidtionDatagridFrom').datagrid('updateRow', {
                    index: rowIndex,
                    row: {flag: 'liquidationIng'}
                });

                if ("liquidationIng" == row.flag) {

                    $.post(getRootPath() + "/accountLiquidation/modifycachevalue?time=" + row.time);
                }

            }
        });

    }

}

//清除缓存的清算结果数据
function clearliquidationHistoryDay() {

    //$('clearliquidationHistoryDay').linkbutton({disabled: false});
    //$('#clearliquidationHistoryDay').linkbutton({disabled: true});
    $.post(getRootPath() + '/accountLiquidation/clearliquidationHistoryDay',
        function (result) {
            if (result) {
                $.messager.alert('提示', "更新成功");
            } else {
                $.messager.alert('错误提示', "更新失败");
            }

        });
}


function queryliquidationHistoryDay(type) {

    var channelName = $("#channelName").combobox("getText");

    if (channelName == "" || channelName == null) {
        $.messager.alert('提示', '通道不能为空');
        return false;
    }

    $("#liquidtionDatagridFrom").datagrid("options").url = "easyui?channelName=" + channelName;
    $("#liquidtionDatagridFrom").datagrid("load");
};


function onReset() {
    $('#fmLiquidation').form('clear');
}


formatterDate = function (date) {
    var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
    var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
    + (date.getMonth() + 1);
    return date.getFullYear() + '-' + month + '-' + day;
};


paramEmpty = function (param) {
    if (parma == "" || parm == null || param.replace(/(^s*)|(s*$)/g, "").length == 0) {
        return true;
    }
}


validatiorDay = function (day) {

    //日期的正则表达式
    var reg = /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/;
    var regExp = new RegExp(reg);
    if (!regExp.test(day)) {
        $.messager.alert('提示', '日期格式出错.eg 2018-01-01');
        return false;
    } else {
        return true;
    }

}


function trim(str) { //删除左右两端的空格
    if (str != null && str.length > 0) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    }
}
function ltrim(str) { //删除左边的空格
    if (str != null && str.length > 0) {
        return str.replace(/(^\s*)/g, "");
    }
}

function rtrim(str) { //删除右边的空格
    if (str != null && str.length > 0) {
        return str.replace(/(\s*$)/g, "");
    }
}


/**
 * 获取路径
 *
 * @returns
 */
function getRootPath() {
    var curWwwPath = window.document.location.href;
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    var localhostPaht = curWwwPath.substring(0, pos);
    var projectName = pathName
        .substring(0, pathName.substr(1).indexOf('/') + 1);
    if (projectName.indexOf('crops') != -1) {
        return (localhostPaht + projectName);
    } else {
        return (localhostPaht);
    }
}